'use strict';

const AV = require("leancloud-storage");
const { Query, User } = AV;

class Forum {
    constructor(APPID, APPKEY, SERVERURL) {
        this.APPID = APPID;
        this.APPKEY = APPKEY;
        this.SERVERURL = SERVERURL;

        AV.init({
            appId: this.APPID,
            appKey: this.APPKEY,
            serverURL: this.SERVERURL
          });

        this.social = AV.Object.extend('social_data');
        this.social_reply = AV.Object.extend('social_reply');
    }

    UserLogin(Username, Password, callbackSuccess, callbackFail) {
        AV.User.logIn(Username, Password).then((UserInfo) => {
            callbackSuccess(UserInfo);
        }, (ErrorMsg) => {
            callbackFail(ErrorMsg);
        });
    }

    UserLogout(callback) {
        AV.User.logOut();
        callback();
    }

    UserRegister(Username, Password, callbackSuccess, callbackFail) {
        let newUser = new AV.User();

        newUser.setUsername(Username);
        newUser.setPassword(Password);

        newUser.signUp().then((newUserInfo) => {
            callbackSuccess(newUserInfo);
        }, (ErrorMsg) => {
            callbackFail(ErrorMsg);
        });
    }

    ChangeUserDescription(newDescription, callbackSuccess, callbackFail) {
        currentUser.set("Description", newDescription);
        currentUser.save().then((newUserInfo) => {
            callbackSuccess(newUserInfo);
        },
            (ErrorMsg) => {
                callbackFail(ErrorMsg);
            }
        );
    }

    GetUserProfile(Username, callbackSuccess, callbackFail) {
        let query = new AV.Query('_User');
        query.equalTo('username', Username);
        query.find().then((UserInfo) => {

            callbackSuccess(UserInfo);
        },
            (ErrorMsg) => {
                callbackFail(ErrorMsg);
            }
        );

    }

    PublishPost(TitleOfPost, ContentOfPost, callbackSuccess, callbackFail) {
        let newPost = new this.social();
        newPost.set('Title', TitleOfPost);
        newPost.set('Sender', AV.User.current());
        newPost.set('Content', ContentOfPost);
        newPost.save().then((newPostInfo) => {
            callbackSuccess(newPostInfo);
        }, (ErrorMsg) => {
            callbackFail(ErrorMsg);
        });
    }

    PublishReply(PostID, ContentOfReply, callbackSuccess, callbackFail) {
        let newReply = new this.social_reply();
        newReply.set('PostID', PostID);
        newReply.set('Sender', AV.User.current());
        newReply.set('Content', ContentOfReply);
        newReply.save().then((newReplyInfo) => {
            callbackSuccess(newReplyInfo);
        },
            (ErrorMsg) => {
                callbackFail(ErrorMsg);
            });
    }

    DeletePost(PostID, callbackSuccess, callbackFail) {
        let thePost = AV.Object.createWithoutData('social_data', PostID);

        thePost.destroy().then(() => {
            callbackSuccess();
        },
            (ErrorMsg) => {
                callbackFail(ErrorMsg);
            }
        );
    }

    DeleteReply(ReplyID, callbackSuccess, callbackFail) {
        let theReply = AV.Object.createWithoutData('social_reply', ReplyID);

        theReply.destroy().then(
            () => { callbackSuccess(); },
            (ErrorMsg) => {
                callbackFail(ErrorMsg);
            }
        );
    }

    GetPostList(callbackSuccess, callbackFail) {
        let query = new AV.Query('social_data');
        query.include("Sender");
        query.find().then((PostListData) => {
            callbackSuccess(PostListData);
        },
            (ErrorMsg) => {
                callbackFail(ErrorMsg);
            }
        );
    }

    GetReplyOfPost(PostID, callbackSuccess, callbackFail) {
        let query = new AV.Query('social_reply');
        query.equalTo('PostID', PostID);
        query.include("Sender");
        query.find().then(
            (ReplyData) => {
                callbackSuccess(ReplyData);
            }
            ,
            (ErrorMsg) => {
                callbackFail(ErrorMsg);
            }
        );
    }

    GetPost(PostID, callbackSuccess, callbackFail) {
        let query = new AV.Query('social_data');
        query.equalTo('objectId', PostID);
        query.include("Sender");
        query.find().then(
            (PostData) => {
                callbackSuccess(PostData);
            }
            ,
            (ErrorMsg) => {
                callbackFail(ErrorMsg);
            }
        );
    }


}

module.exports = Forum;